package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import fm.dongman.animefm.R;

import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.modellib.model.ComicModel;
import fm.dongman.modellib.model.UserModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 人气番剧适配器
 * Created by Administrator on 2017/10/11.
 */

public class HomeHotComicAdapter extends RecyclerView.Adapter<HomeHotComicAdapter.ViewHolder> {
    private UserModel mUser;
    private Context mContext;
    private List<IModel.IComicModel> mListComic;

    public HomeHotComicAdapter(List<IModel.IComicModel> ListComic, Context context) {
        this.mContext = context;
        this.mListComic = ListComic;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView mHotPm;     //排名
        private ImageView mImgUrl;   //图片
        private TextView mTitle;     //标题
        private TextView mComicType; //动漫类型
        private View mColorBg;
        private View mColorMark;

        public ViewHolder(View itemView) {
            super(itemView);
            mImgUrl = (ImageView) itemView.findViewById(R.id.mIvHotRankingPic);
            mTitle = (TextView) itemView.findViewById(R.id.mTvHotRankingTitle);
            mComicType = (TextView) itemView.findViewById(R.id.mTvHotRankingClassify1);
            mHotPm = (TextView) itemView.findViewById(R.id.mTvHotRanking);
            mColorBg = itemView.findViewById(R.id.mVColorBg);
            mColorMark = itemView.findViewById(R.id.mVColorMark);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_hot_anime, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ComicModel comic = (ComicModel) mListComic.get(position);
        holder.mHotPm.setText(String.valueOf("0" + (position + 1)));
        HotComicRanking(holder, position); //设置字体颜色
        Glide.with(mContext)
                .load(comic.getImgUrl())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 2))
                .into(holder.mImgUrl);
        holder.mTitle.setText(comic.getTitle());
        holder.mComicType.setText("恋爱/后宫/泡面/伪娘/模拟/数据");


        //单击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof Activity) {
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_ANIME_INFO());
                    intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), comic.getId());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                } else {
                    throw new RuntimeException(mContext.toString() + "必须是Activity!");
                }
            }
        });
    }

    /**
     * 热漫排行文字变色
     *
     * @param holder
     * @param position
     */
    public void HotComicRanking(ViewHolder holder, int position) {
        int color = 0;
        if (position == 0) {
            color = ContextCompat.getColor(mContext, R.color.hot_comic_red);
        } else if (position == 1) {
            color = ContextCompat.getColor(mContext, R.color.hot_comic_orange);
        } else if (position == 2) {
            color = ContextCompat.getColor(mContext, R.color.hot_comic_green);
        } else {
            color = Color.parseColor("#555555");
            holder.mColorBg.setBackgroundColor(Color.parseColor("#ffffffff"));
            holder.mColorMark.setBackgroundColor(Color.parseColor("#ffffffff"));
            holder.mHotPm.setTextColor(color);
            return;
        }
        holder.mColorBg.setBackgroundColor(color);
        holder.mColorMark.setBackgroundColor(color);
        holder.mHotPm.setTextColor(color);
    }

    @Override
    public int getItemCount() {
        return mListComic.size();
    }


}
